using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace Magiq.Databases.Insert
{
    public class InsertCommandBuilder : DynamicSelectCommandBuilder
    {
        public InsertCommandBuilder(Type elementType, IDatabaseAdapter databaseAdapter, IQueryable where, IEnumerable<InsertColumnsAssignment> assignments)
            : base(databaseAdapter, where, assignments.Cast<ColumnsAssignment>(), false)
        {
            Init(elementType);
        }

        public override IDbCommand GetCommand()
        {
            var values = BaseSelectedValues;
            var columns = Columns;

            var columnValues = new Dictionary<string, string>();

            for (var i = 0; i < values.Length; i++)
                columnValues.Add(columns[i], values[i]);

            Command.CommandText = DatabaseAdapter.Dialect.InsertClauseFor(BaseTableName, columnValues, BaseWhereClause);

            return Command;
        }
    }
}